package com.dstz.demo.core.manager.impl;


import com.dstz.demo.core.dao.ProjectDao;
import com.dstz.demo.core.manager.ProjectManager;
import com.dstz.demo.core.model.ProjectBudget;
import com.dstz.demo.core.model.ProjectDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("projectManager")
public class ProjectManagerImpl implements ProjectManager {

    @Autowired(required = false)
    ProjectDao projectDao;

    //查询项目详情表中数据
    @Override
    public Map selectOne(String id) {
        return projectDao.selectOne(id);
    }

    //添加预算单详情
    @Override
    public List<ProjectDetails> uploadTwo(List<ProjectDetails> lists) {
        projectDao.insertOne(lists);
        return lists;
    }

    //根据预算的id，查询所有的预算详情信息
    @Override
    public List<ProjectDetails> projectDetails(String id) {
        return projectDao.projectDetails(id);
    }

    @Override
    public void submit(String id, ProjectBudget projectBudget) {
        projectBudget.setId(Integer.parseInt(id));
        //更新
        projectDao.updateProjectDetails(projectBudget);
    }

    //初始化预算单
    @Override
    public Integer createProjectdubget() {
        HashMap<String, Integer> map = new HashMap<>();
        projectDao.createProjectdubget(map);
        return map.get("id");
    }

    //这个id是预算单id
    @Override
    public Map<String, List> appendReduce(String id, List<ProjectDetails> lists) {
        //根据预算单id查询出来之前添加的预算单详情
        HashMap<String, List> map = new HashMap<>();
        List<ProjectDetails> update = new ArrayList<>();
        List<ProjectDetails> zhengchang = new ArrayList<>();
        List<ProjectDetails> canzhao=new ArrayList<>();
        List<ProjectDetails> projectDetails = projectDao.projectDetails(id);
        System.out.println("集合的size是：" + projectDetails.size());
        int size = 0;
        // 初始化大小
        if (projectDetails.size() > lists.size()) {
            size = projectDetails.size();
        } else if (projectDetails.size() < lists.size()) {
            size = lists.size();
        } else if (projectDetails.size() == lists.size()) {
            size = lists.size();
        }
        for (int i = 0; i < size; i++) {
            ProjectDetails news = lists.get(i);
            int num = 0;
            for (int j = 0; j < size; j++) {
                num = 0;
                ProjectDetails old = projectDetails.get(i);
                //如果这个两个的任务编码相等了，就在判断他其他的值是否相等
                if (old.getTaskNumber().equals(news.getTaskNumber())) {
                    if (old.equals(news)) {
                        //如果任务编码一样，eq也一样这个数据就不需要做追加或者替换
                        ++num;
                    } else {
                        //如果任务编码一样，但是eq不一样就说明是需要追加或者替换的
                        news.setId(old.getId());
                    }
                }
            }
            //如果num大于0，这个数据就是正常的，说明新添加的数据和数据库中的数据是一直的
            //需要追加或者替换
            if (num > 0) {
                zhengchang.add(news);
            } else if (num == 0) {
                //如果说数据中，没有一个是跟他相等的 ，那就说明这个数据追加或者替换了
                update.add(news);
            }
        }
        System.out.println("需要更新的数据" + update.size());
        System.out.println("新数据的" + zhengchang.size());
        //编辑可以替换或者更新的预算单详情
        for (int i = 0; i < update.size(); i++) {

            ProjectDetails project = update.get(i);
            Integer id1 = project.getId();
            //拿着这个id去查询对应的数据
            ProjectDetails project1=projectDao.selectByidDetails(id1);
            canzhao.add(project1);
        }
        map.put("update", update);
        map.put("zhengchang", zhengchang);
        map.put("canzhao",canzhao);
        return map;
    }

    //追加减提交请求
    @Override
    public void sumbitAppendReduce(List<Map> maps) {
        for (int i = 0; i < maps.size(); i++) {
            Map map = maps.get(i);
            String append = (String) map.get("append");
            Integer id = (Integer) map.get("id");
            //追加
            if (append != null && "1".equals(append)) {
                //根据id查询预算单详情
                ProjectDetails detail = projectDao.append(id);
                String number = (String) map.get("number");//数量
                String hours = (String) map.get("hours");// 人工工时
                String cost = (String) map.get("cost");// 材料分包
                String cl = (String) map.get("cl"); //  材料
                String fb = (String) map.get("fb");  //  分包
                String qt = (String) map.get("qt");  //其他
                String hj = (String) map.get("hj");//合计
                String se = (String) map.get("se"); //税额
                String hshj = (String) map.get("hshj"); //含税合计
                //追加
                if ("".equals(number) || number==null) {
                    number = "0";
                }
                if ("".equals(hours) || hours==null){
                    hours = "0";
                }
                if ("".equals(cost)|| cost==null) {
                    cost = "0";
                }
                if ("".equals(cl)|| cl==null) {
                    cl = "0";
                }
                if ("".equals(fb)|| fb==null) {
                    fb = "0";
                }
                if ("".equals(qt)|| qt==null) {
                    qt = "0";
                }
                if ("".equals(hj)||hj ==null) {
                    hj = "0";
                }
                if ("".equals(se)||se ==null) {
                    se = "0";
                }
                if ("".equals(hshj)||hshj ==null) {
                    hshj = "0";
                }
                if ("".equals(detail.getNumber()) || detail.getNumber()==null) {
                    detail.setNumber("0");
                }
                if ("".equals(detail.getHours()) || detail.getHours()==null) {
                    detail.setHours("0");
                }
                if ("".equals(detail.getCost()) || detail.getCost()==null) {
                    detail.setCost("0");
                }
                if ("".equals(detail.getCl()) || detail.getCl()==null) {
                    detail.setCl("0");
                }
                if ("".equals(detail.getFb()) || detail.getFb()==null) {
                    detail.setFb("0");
                }
                if ("".equals(detail.getQt()) || detail.getQt()==null) {
                    detail.setQt("0");
                }
                if ("".equals(detail.getHj()) || detail.getHj()==null) {
                    detail.setHj("0");
                }
                if ("".equals(detail.getSe()) || detail.getSe()==null) {
                    detail.setSe("0");
                }
                if ("".equals(detail.getHshj()) || detail.getHshj() == null) {
                    detail.setHshj("0");
                }
                Double number1 = Double.parseDouble(number) + Double.parseDouble(detail.getNumber());
                Double hours1 = Double.parseDouble(hours) + Double.parseDouble(detail.getHours());
                Double cost1 = Double.parseDouble(cost) + Double.parseDouble(detail.getCost());
                Double cl1 = Double.parseDouble(cl) + Double.parseDouble(detail.getCl());
                Double fb1 = Double.parseDouble(fb) + Double.parseDouble(detail.getFb());
                Double qt1 = Double.parseDouble(qt) + Double.parseDouble(detail.getQt());
                Double hj1 = Double.parseDouble(hj) + Double.parseDouble(detail.getHj());
                Double se1 = Double.parseDouble(se) + Double.parseDouble(detail.getSe());
                Double hshj1 = Double.parseDouble(hshj) + Double.parseDouble(detail.getHshj());
                //追加完成之后设置回去
                detail.setNumber(String.valueOf(number1));
                detail.setHours(String.valueOf(hours1));
                detail.setCost(String.valueOf(cost1));
                detail.setCl(String.valueOf(cl1));
                detail.setFb(String.valueOf(fb1));
                detail.setQt(String.valueOf(qt1));
                detail.setHj(String.valueOf(hj1));
                detail.setSe(String.valueOf(se1));
                detail.setHshj(String.valueOf(hshj1));
                //根据id追加数量
                projectDao.abppend(detail);
            } else if (append != null && "2".equals(append)) {
                //2.替换
                ProjectDetails detail = projectDao.append(id);
                String number = (String) map.get("number");//数量
                String hours = (String) map.get("hours");// 人工工时
                String cost = (String) map.get("cost");// 材料分包
                String cl = (String) map.get("cl"); //  材料
                String fb = (String) map.get("fb");  //  分包
                String qt = (String) map.get("qt");  //其他
                String hj = (String) map.get("hj");//合计
                String se = (String) map.get("se"); //税额
                String hshj = (String) map.get("hshj"); //含税合计
                detail.setNumber(number);
                detail.setHours(hours);
                detail.setCost(cost);
                detail.setCl(cl);
                detail.setFb(fb);
                detail.setQt(qt);
                detail.setHj(hj);
                detail.setSe(se);
                detail.setHshj(hshj);
                projectDao.abppend(detail);
            }
        }

    }

    //查询预算单中所有的预算单详情
    @Override
    public List<ProjectDetails> selectAll(String id) {
        return projectDao.selectAll(id);
    }

    @Override
    public void updateVersion(String id, String version) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id",id);
        map.put("version",version);
        projectDao.updateVersion(map);
    }

    //审核通过接口
    @Override
    public void updateStatus(String[] split) {
        List<String> strings = Arrays.asList(split);
        projectDao.updateStatus(strings);
        //查询出来所有的预算单id
        Integer id = projectDao.selectprojectId(strings.get(1));
        //根据预算单id查询出来所有的预算单详情
        List<ProjectDetails> projectDetails = projectDao.selectAll(id.toString());
        int num=0;
        for (int i = 0; i < projectDetails.size(); i++) {
            //如果有一个预算单详情没有通过就让num加上1
            if(projectDetails.get(i).getStatus()!="2"){
                ++num;
            }
        }
        //如果所有的都审核通过了
        if(num==0){
            //就让预算到单审核通过
            projectDao.updateProjectdubget(id);
        }

    }

    //审核不通过接口
    @Override
    public void updateStatusNO(String note, String[] split) {
        List<String> strings = Arrays.asList(split);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", strings);
        map.put("note", note);
        projectDao.updateStatusNO(map);
    }

}
